﻿using System;
using System.Collections.Generic;
using TuxedoLib;

namespace SpaceSquirrel
{
	public class TitleScene : GameSceneBase
	{
		private int flashCounter = 0;
		private string[][] options = new string[][] {
			"start|Start Game".Split('|'),
			"password|Enter Password".Split('|'),
			"joystick|Configure Game Pad".Split('|')
		};
		private int index = 0;

		public TitleScene()
			: base()
		{ }

		public override void ProcessInput(List<MyEvent> events, Dictionary<string, bool> pressedActions)
		{
			foreach (MyEvent ev in events)
			{
				if (this.flashCounter < 0)
				{
					if (ev.Down)
					{
						if (ev.Action == "start" || ev.Action == "A" || ev.Action == "B")
						{
							this.flashCounter = 30;
							SoundManager.PlayNoise("menu_select");
						}
						else if (ev.Action == "up")
						{
							if (this.index > 0)
							{
								this.index--;
								SoundManager.PlayNoise("menu_move");
							}
						}
						else if (ev.Action == "down")
						{
							if (this.index < 2)
							{
								this.index++;
								SoundManager.PlayNoise("menu_move");
							}
						}
					}
				}
			}
		}

		public override void Update()
		{
			this.flashCounter--;
			if (this.flashCounter == 1)
			{
				string action = this.options[this.index][0];
				if (action == "start")
				{
					IntroScene introScene = new IntroScene();
					this.Next = introScene;
					this.Next = introScene.GetStartScene();
				}
				else if (action == "password")
				{
					this.Next = new PasswordScene();
				}
				else if (action == "joystick")
				{
					//this.Next = new JoystickMenuScene();
				}
				else
				{
					throw new InvalidOperationException("Invalid option.");
				}
			}
		}

		public override void Render(Image screen, int renderCounter)
		{
			JukeBox.EnsureSong("title");

			screen.Fill(0, 0, 0);
			screen.Blit(ImageLibrary.GetImage("slides/title.png"), 0, 0);

			bool showText = true;
			if (this.flashCounter > 0)
			{
				showText = renderCounter % 4 < 2;
			}

			int index = 0;
			int y = 136;
			int left = 64;

			SquirrelUtil.DrawBox(screen, left - 32, y - 16, 24, 9);

			foreach (string[] option in this.options)
			{
				if (index == this.index)
				{
					screen.Blit(Text.Render(Draw.CreateColor(255, 255, 255), ">"), left - 16, y);
				}

				if (index != this.index || showText)
				{
					screen.Blit(Text.Render(Draw.CreateColor(255, 255, 255), option[1]), left, y);
				}

				index++;
				y += 16;
			}

			y += 32;
			Image activeJs = Text.Render(Draw.CreateColor(128, 128, 128), "Active joystick: ");
			screen.Blit(activeJs, 8, y);
			int x = 8 + activeJs.Width;
			Image jsName;
			if (JoystickHelper.ActiveJoystick == null)
			{
				jsName = Text.Render(Draw.CreateColor(255, 0, 0), "None");
			}
			else
			{
				jsName = Text.Render(Draw.CreateColor(255, 255, 255), JoystickHelper.ActiveJoystick);
			}

			screen.Blit(jsName, x, y);
		}
	}
}
